
package edu.vt.ece_3574.amber_alert;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import edu.vt.ece_3574.amber_alert.AlertSettings.Battery;
import edu.vt.ece_3574.amber_alert.AlertSettings.PollingInterval;
import edu.vt.ece_3574.amber_alert.accounts.Account;
import edu.vt.ece_3574.amber_alert.location.LocationManagerHandler;
import edu.vt.ece_3574.amber_alert.membership.AccountSettingsLoginActivity;
import edu.vt.ece_3574.amber_alert.membership.HangoutAdderActivity;
import edu.vt.ece_3574.amber_alert.membership.PrimaryAdderActivity;
import edu.vt.ece_3574.amber_alert.messaging.MessagingUtils;

public class MemberMain extends Activity {
	private static final String TAG = MemberMain.class.getSimpleName();
	
	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		ensurePrimaryRegistered();

		setContentView(R.layout.member_main);

		//Create the button click listener for the myaccount button to start the MyAccount activity
		Button myAccountButton = (Button)findViewById(R.id.myaccount);
		myAccountButton.setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				Intent i = new Intent(MemberMain.this, AccountSettingsLoginActivity.class);
				startActivity(i);
				finish();
			}
		});

		//Create the button click listener for the distress button to send a distress signal message
		Button sendDistressButton = (Button)findViewById(R.id.distress);
		sendDistressButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String distressMessage = "Help!! I've been kidnapped";
				ArrayList<Account> primaries = AmberAlertApplication.get().getCurrentAccount().getAssociatedAccounts();
				for (Account primaryNumber : primaries) {
					MessagingUtils.get().sendSMS(primaryNumber.getNumber(), distressMessage);
				}
			}
		});
		
		Button useOldHangout = (Button)findViewById(R.id.existing_hang_button);
		useOldHangout.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				Intent i = new Intent(MemberMain.this, ExistingHangoutActivity.class);
				startActivity(i);
				finish();
			}
			
		});
		if (AmberAlertApplication.get().getCurrentAccount().getAssociatedFriends().isEmpty()) {
			useOldHangout.setEnabled(false);
		}
		else {
			useOldHangout.setEnabled(true);
		}

		//Create the button click listener for the low battery alert
		Button batlevel = (Button)findViewById(R.id.battery);
		batlevel.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				int setLevel = Battery.getSetLevel();
				String Level;


				if(Battery.BatLevel()){
					Level = "Battery is above " + setLevel + " %";
				}
				else{
					Level = "Battery is less than " +setLevel + " %";
				}

				ArrayList<Account> primaries = AmberAlertApplication.get().getCurrentAccount().getAssociatedAccounts();
				for (Account primaryNumber : primaries) {
					MessagingUtils.get().sendSMS(primaryNumber.getNumber(), Level);
				}
			}
		});

		Button hangoutPress = (Button)findViewById(R.id.starthangout);
		hangoutPress.setOnClickListener(new OnClickListener() {
			public void onClick(View v){
				Intent i = new Intent(MemberMain.this, HangoutAdderActivity.class);
				startActivity(i);
				finish();
			}
		});

		//Start the location manager which will persist throughout the runtime of the application
		LocationManagerHandler.start();
		startBackgroundService();
	}

	private void ensurePrimaryRegistered() {
		if (AmberAlertApplication.get().getCurrentAccount().getAssociatedAccountNames().size() == 0) {
			Intent i = new Intent(MemberMain.this, PrimaryAdderActivity.class);
			startActivity(i);
			MemberMain.this.finish();
		}
	}
	
	private void startBackgroundService() {
		SharedPreferences intervalSettings = getSharedPreferences(AmberAlertApplication.defaultInterval, 0);
		String intervalString = intervalSettings.getString("interval", "NotSet");
		if(intervalString.equals("NotSet")) {
			PollingInterval.setPollInterval(PollingInterval.defaultInterval);
			SharedPreferences.Editor editor = intervalSettings.edit();
			editor.putString("interval", Integer.toString(PollingInterval.defaultInterval));
			editor.commit();
		}
		else
			PollingInterval.setPollInterval(Integer.parseInt(intervalString));
		
		//Start background service when program first starts on a member phone
		Log.d(TAG, "before startService");
		if(!BackgroundService.serviceStarted){
			BackgroundService.serviceStarted = true;
			startService(new Intent(this, BackgroundService.class));
		}
		Log.d(TAG, "after startService");
	}
}